// const httpHelper=require('./httpHelper');
const cheerio = require('cheerio');
const fetchUrl = require('fetch').fetchUrl;
//const iconv = require('iconv-lite');

//读取网址并解析成jQuery格式
function read(html, options, callback) {
    if (typeof options === 'function') {
        callback = options;
        options = {};
    }

    if (html.indexOf('<') === -1) {
        fetchUrl(html, options, jsdomParse);
    } else {
        jsdomParse(null, null, html);
    }

    function jsdomParse(error, meta, body) {
        if (error) {
            return callback(error);
        }

        if (typeof body !== 'string') body = body.toString();
        //var html=iconv.decode(body,'gbk');

        var $=cheerio.load(body, { decodeEntities:false});
        //var $ = cheerio.load(body);
        if (!$) {
            callback(new Error('parse html error'), null);
        } else {
            callback(null, $);
        }
    }
}

//获取一本小说信息
let getOneNovel=async function(url){
    read(url,{outputEncoding:'utf8'}, function(err, $) {
        console.log($('body').html());
    });
};

//获取一个章节的信息
let getOneSection=async function(url){

};

let readPromise=function(url){
    return new Promise((resolve, reject)=>{
        read(url,(err,data)=>{
            if(!err){
                resolve(data);
            }else{
                reject(err);
            }
        });
    });
};

//getOneNovel('https://www.biquge5.com/3_3194/');


let novelThief={
    run:async function(){
        //let novelList=await getNovelList();
        let urlArray=[];
        //共计2177本小说
        for(let i=1;i<=1;i++){
            try{
                let $=await readPromise(`https://www.biquge5.com/shuku/0/allvisit-0-${i}.html`);
                let $listGroup=$('body').find('.list-group');
                let $urlArray=$listGroup.find('.col-xs-9 a');
                for(let j=0;j<$urlArray.length;j++){
                    urlArray.push($urlArray[j].attribs.href);
                }
                //$urlArray.concat($listGroup.find('.col-xs-9 a'));
                //console.log($listGroup.find('.col-xs-9 a').length);
            }catch(e){
                console.log(e);
            }
        }
        for(let i=0;i<1;i++){
            let $=await readPromise(urlArray[i]);
            console.log($('body').find('._chapter li a').length);
        }
        //console.log($urlArray[0].html());
    },
    refresh:async function(){

    }
};
novelThief.run();


module.exports=novelThief;